package com.beidasoft.web.config;


import com.beidasoft.web.interceptor.CommonInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.http.CacheControl;
import org.springframework.web.servlet.config.annotation.*;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import org.springframework.web.servlet.view.JstlView;

import java.io.File;
import java.util.concurrent.TimeUnit;

/**
 * Created by hoax on 2021-02-22 09:00.
 */
@Configuration
@EnableWebMvc
public class MvcConfig implements WebMvcConfigurer {

    @Bean
    public CommonInterceptor commonInterceptor() {
        return new CommonInterceptor();
    }

    @Bean
    public InternalResourceViewResolver resourceViewResolver() {
        InternalResourceViewResolver resolver = new InternalResourceViewResolver();
        resolver.setViewClass(JstlView.class);
        //请求视图文件的前缀地址
        resolver.setPrefix("/WEB-INF/jsp/");
        //请求视图文件的后缀
        resolver.setSuffix(".jsp");
        return resolver;
    }

    /**
     * 视图配置
     *
     * @param registry
     */
    @Override
    public void configureViewResolvers(ViewResolverRegistry registry) {
        registry.viewResolver(this.resourceViewResolver());
        registry.jsp("/WEB-INF/jsp/", ".jsp");
    }

    /**
     * 重写接口中的addInterceptors方法，添加自定义拦截器
     *
     * @param registry
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 通过registry来注册拦截器，通过addPathPatterns来添加拦截路径,通过excludePathPatterns添加不拦截路径
        registry.addInterceptor(this.commonInterceptor())
                .addPathPatterns("/**")
                .excludePathPatterns("/plugins/**")
                .excludePathPatterns("/config/**")
                .excludePathPatterns("/scripts/**")
                .excludePathPatterns("/css/**")
                .excludePathPatterns("/img/**")
                .excludePathPatterns("/download/**")
                .excludePathPatterns("/reportJsp/**")
                .excludePathPatterns("/raqsoft/**")
                .excludePathPatterns("/js/**")
                .excludePathPatterns("/caseManager/caseManagerES/**")
                .excludePathPatterns("/css/**");
    }


    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/").setViewName("forward:login.jsp");
        registry.setOrder(Ordered.HIGHEST_PRECEDENCE);
        // "/login" 在controller里有映射,跳转login页面
        //该句等同于@RequestMapping("/")  return "login"
    }

    /**
     * 修改springboot中默认的静态文件路径
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        //addResourceHandler请求路径
        //addResourceLocations 在项目中的资源路径
        //setCacheControl 设置静态资源缓存时间
        registry.addResourceHandler("/scripts/**").addResourceLocations("classpath:/WEB-INF/scripts/")
                .setCacheControl(CacheControl.maxAge(1, TimeUnit.HOURS).cachePublic());
        registry.addResourceHandler("/config/**").addResourceLocations("classpath:/WEB-INF/config/")
                .setCacheControl(CacheControl.maxAge(1, TimeUnit.HOURS).cachePublic());
        registry.addResourceHandler("/plugins/**").addResourceLocations("classpath:/WEB-INF/plugins/")
                .setCacheControl(CacheControl.maxAge(1, TimeUnit.HOURS).cachePublic());
        registry.addResourceHandler("/css/**").addResourceLocations("classpath:/WEB-INF/css/")
                .setCacheControl(CacheControl.maxAge(1, TimeUnit.HOURS).cachePublic());
        registry.addResourceHandler("/img/**").addResourceLocations("classpath:/WEB-INF/img/")
                .setCacheControl(CacheControl.maxAge(1, TimeUnit.HOURS).cachePublic());
        registry.addResourceHandler("/download/**").addResourceLocations("classpath:/WEB-INF/download/")
                .setCacheControl(CacheControl.maxAge(1, TimeUnit.HOURS).cachePublic());
        registry.addResourceHandler("/jsp/**").addResourceLocations("classpath:/WEB-INF/jsp/")
                .setCacheControl(CacheControl.maxAge(1, TimeUnit.HOURS).cachePublic());
        registry.addResourceHandler("/reportJsp/**").addResourceLocations("classpath:/reportJsp/")
                .setCacheControl(CacheControl.maxAge(1, TimeUnit.HOURS).cachePublic());
        registry.addResourceHandler("/raqsoft/**").addResourceLocations("classpath:/WEB-INF/raqsoft/")
                .setCacheControl(CacheControl.maxAge(1, TimeUnit.HOURS).cachePublic());
        File personUpload =new File("D:/zfpt02/person/upload");    //如果文件夹不存在则用c盘
        if (!personUpload.exists() && !personUpload.isDirectory()) {
            registry.addResourceHandler("/file/img/**").addResourceLocations("file:C:/zfpt02/person/upload/")
                    .setCacheControl(CacheControl.maxAge(1, TimeUnit.HOURS).cachePublic());
        } else {
            registry.addResourceHandler("/file/img/**").addResourceLocations("file:D:/zfpt02/person/upload/")
                    .setCacheControl(CacheControl.maxAge(1, TimeUnit.HOURS).cachePublic());
        }
        File personTemporary =new File("D:/zfpt02/person/temporary");    //如果文件夹不存在则用c盘
        if (!personTemporary.exists() && !personTemporary.isDirectory()) {
            registry.addResourceHandler("/file/imgTemporary/**").addResourceLocations("file:C:/zfpt02/person/temporary/")
                    .setCacheControl(CacheControl.maxAge(1, TimeUnit.HOURS).cachePublic());
        } else {
            registry.addResourceHandler("/file/imgTemporary/**").addResourceLocations("file:D:/zfpt02/person/temporary/")
                    .setCacheControl(CacheControl.maxAge(1, TimeUnit.HOURS).cachePublic());
        }
    }

}
